package com.bhs.sansong.adapter;

import static android.content.Context.CLIPBOARD_SERVICE;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;


import com.bhs.sansong.R;
import com.bhs.sansong.base.BaseActivity;
import com.bhs.sansong.base.BaseBean;
import com.bhs.sansong.bean.CircleFriendsBean;
import com.bhs.sansong.bean.JudgeListDTO;
import com.bhs.sansong.net.NetCallBack;
import com.bhs.sansong.ui.dialog.CommentProcessingDialog;
import com.bhs.sansong.ui.dialog.MyClickListener;
import com.bhs.sansong.ui.dialog.VideoDialog;
import com.bhs.sansong.ui.message.CirclePersonageActivity;
import com.bhs.sansong.utils.MyUtils;
import com.bhs.sansong.utils.SharedPreferencesUtils;
import com.bhs.sansong.utils.ToastUtil;
import com.bhs.sansong.view.AutoLineFeedLayoutManager;
import com.bhs.sansong.view.PraiseListView;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.util.SmartGlideImageLoader;

import java.util.Collections;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Response;

public class CircleFriendsAdapter extends BaseQuickAdapter<CircleFriendsBean, BaseViewHolder> implements NetCallBack {
    private ClipData mClipData;   //剪切板Data对象
    private ClipboardManager mClipboardManager;   //剪切板管理工具类
    private BaseActivity activity;
    private int mPosition;
    private int index;
    private String uid;
    private int type = 1;//1圈子列表  2详情

    //详情页调用
    public CircleFriendsAdapter(int layoutResId, BaseActivity activity) {
        super(layoutResId);
        this.activity = activity;
        mClipboardManager = (ClipboardManager) activity.getSystemService(CLIPBOARD_SERVICE);
        uid = SharedPreferencesUtils.getString(mContext, "uid");
        type = 2;
    }

    public CircleFriendsAdapter(BaseActivity activity) {
        super(R.layout.item_circle_friends);
        this.activity = activity;
        mClipboardManager = (ClipboardManager) activity.getSystemService(CLIPBOARD_SERVICE);
        uid = SharedPreferencesUtils.getString(mContext, "uid");
    }

    @Override
    protected void convertPayloads(@NonNull BaseViewHolder helper, CircleFriendsBean item, @NonNull List<Object> payloads) {
        super.convertPayloads(helper, item, payloads);
        myConvert(helper, item);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, CircleFriendsBean item) {
        Glide.with(mContext).load(item.getHeaderpic()).into((ImageView) helper.getView(R.id.iv_head));
        RecyclerView mRvList = helper.getView(R.id.rv_list);
        if (item.getResource().size() == 1) {
            //图片只有一张时显示一列
            mRvList.setLayoutManager(new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL));
        } else if (item.getResource().size() == 2) {
            //图片两张或者四张时显示两列
            mRvList.setLayoutManager(new LinearLayoutManager(mContext, RecyclerView.HORIZONTAL, false));
        } else if (item.getResource().size() == 4) {
            //图片两张或者四张时显示两列
            mRvList.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.HORIZONTAL));
        } else {
            //其他数量图片显示三列
            mRvList.setLayoutManager(new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL));
        }
        PictureShowAdapter2 mAdapter = new PictureShowAdapter2(item.getResource(), item.getResource().size());
        mRvList.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (item.getResource_type() == 0) {
                    new XPopup.Builder(mContext)
                            .asImageViewer(view.findViewById(R.id.iv_picture), position, item.getResource(), null, new SmartGlideImageLoader())
                            .show();
                } else {
                    VideoDialog videoDialog = new VideoDialog(mContext, (String) item.getResource().get(0));
                    videoDialog.show();
                }
            }
        });
        if (type == 1) {
            //倒序
            Collections.reverse(item.getAgreeList());
        }
        myConvert(helper, item);
    }

    private void myConvert(@NonNull BaseViewHolder helper, CircleFriendsBean item) {
        helper.setGone(R.id.iv_play, item.getResource_type() == 1);
        if (item.getLogistics_id() > 0) {
            helper.setText(R.id.tv_nick_name, TextUtils.isEmpty(item.getCompany_name()) ? "" : item.getCompany_name());
        } else {
            helper.setText(R.id.tv_nick_name, TextUtils.isEmpty(item.getNickname()) ? "" : item.getNickname());
        }
        helper.setVisible(R.id.tv_delete, item.getUnid().equals(uid) && type == 1);
        helper.setText(R.id.tv_content, item.getWords());
        helper.setGone(R.id.tv_location, !TextUtils.isEmpty(item.getLocation()));
        helper.setText(R.id.tv_location, item.getLocation());
        helper.setText(R.id.tv_comment, item.getJudgeCount() > 0 ? String.valueOf(item.getJudgeCount()) : " ");
        helper.setText(R.id.tv_like, String.valueOf(item.getAgreeCount()));
        helper.setVisible(R.id.tv_like, item.getAgreeCount() > 0);
        helper.setText(R.id.tv_time, MyUtils.getBefore(item.getCreate_time()));
        helper.setGone(R.id.ll_like, item.getAgreeCount() > 0);
        helper.setGone(R.id.tv_company_details, item.getLogistics_id() > 0);
        helper.setImageResource(R.id.iv_like, item.getIsAgreed() == 0 ? R.mipmap.ic_praise : R.mipmap.ic_praise_blue);
        helper.addOnClickListener(R.id.tv_comment, R.id.tv_like, R.id.fl_comment, R.id.fl_like, R.id.tv_company_details
                , R.id.iv_head, R.id.tv_delete);

        if (type == 2) {
            RecyclerView rvPraise = helper.getView(R.id.rv_praise);
            rvPraise.setAdapter(new PraiseAdapter(item.getAgreeList()));
            rvPraise.setLayoutManager(new AutoLineFeedLayoutManager());
        } else {
            if (!MyUtils.isEmpty(item.getAgreeList())) {
                PraiseListView tvPraise = helper.getView(R.id.tv_Praise);
                tvPraise.setData(item.getAgreeList());
                tvPraise.setOnItemClickListener(new PraiseListView.OnItemClickListener() {
                    @Override
                    public void onClick(int position) {
                        mContext.startActivity(new Intent(mContext, CirclePersonageActivity.class)
                                .putExtra("uid", item.getAgreeList().get(position).getUnid())
                                .putExtra("logistics_id", item.getAgreeList().get(position).getLogistics_id()));
                    }
                });
            }
        }
        RecyclerView rvComment = helper.getView(R.id.rv_comment);
        CommentAdapter commentAdapter = new CommentAdapter(item.getJudgeList());
        rvComment.setAdapter(commentAdapter);
        rvComment.setLayoutManager(new LinearLayoutManager(mContext, RecyclerView.VERTICAL, true));
        rvComment.setVisibility(item.getJudgeList().size() > 0 ? View.VISIBLE : View.GONE);
        commentAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                JudgeListDTO data = item.getJudgeList().get(position);
                CommentProcessingDialog processingDialog = new CommentProcessingDialog(mContext, data.getUnid());
                processingDialog.setMyClickListener(new MyClickListener() {
                    @Override
                    public void onClick(View v) {
                        processingDialog.dismiss();
                        if (v.getId() == R.id.tv_copy) {
                            //创建一个新的文本clip对象
                            mClipData = ClipData.newPlainText("伞送", data.getWords());
                            //把clip对象放在剪贴板中
                            mClipboardManager.setPrimaryClip(mClipData);
                            ToastUtil.show("复制成功");
                        } else if (v.getId() == R.id.tv_delete) {
                            //删除评论
                            mPosition = helper.getLayoutPosition();
                            index = position;
                            activity.networkRequest(activity.netApi.circleDelJudge(item.getId(), data.getCreate_time()), CircleFriendsAdapter.this);
                        }
                    }
                });
                //点击评论弹窗
                new XPopup.Builder(mContext)
                        .atView(view)
                        .isDestroyOnDismiss(true)
                        .hasShadowBg(false) // 去掉半透明背景
                        .asCustom(processingDialog)
                        .show();

            }
        });
    }

    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        BaseBean dataBean = new Gson().fromJson(json, BaseBean.class);
        if (dataBean.getCode().equals("1")) {
            ToastUtil.show("删除成功");
            getData().get(mPosition).setJudgeCount(getData().get(mPosition).getJudgeCount() - 1);
            getData().get(mPosition).getJudgeList().remove(index);
            notifyItemChanged(mPosition, 0);
        } else {
            ToastUtil.show("删除失败");
        }
    }

    @Override
    public void onFailure(int requestCode) {

    }
}
